package chan.project.smartlaweveryman.strategy.register;

import chan.project.smartlawcommon.common.ErrorCode;
import chan.project.smartlawcommon.exception.BusinessException;
import chan.project.smartlaweveryman.service.UserRoleService;
import chan.project.smartlaweveryman.service.impl.UserInfoServiceImpl;
import chan.project.smartlawmodel.dto.user.UserInfoRegisterRequest;
import chan.project.smartlawmodel.entity.LawyerInfo;
import chan.project.smartlawmodel.entity.UserInfo;
import chan.project.smartlawmodel.entity.UserRole;
import chan.project.smartlawmodel.enums.UserRoleEnum;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;
import chan.project.smartlaweveryman.mapper.UserInfoMapper;

@Component
public class DefaultRegisterStrategy implements RegisterStrategy{

    public static final String SALT = "yupi";

    @Resource
    private UserInfoMapper userInfoMapper;

    @Resource
    private UserRoleService userRoleService;

    @Override
    public boolean hasRoleOrRegister(UserInfoRegisterRequest userInfoRegisterRequest) {
        String userAccount = userInfoRegisterRequest.getUserAccount();
        String userPassword = userInfoRegisterRequest.getUserPassword();
        String checkPassword = userInfoRegisterRequest.getCheckPassword();
        //当前用户是否已经注册基本用户
        boolean b = userInfoMapper.hasRole(userAccount);
        if(!b){
            this.userRegister(userAccount,userPassword,checkPassword);
        }
        return b;
    }

    @Override
    public Long saveUser(UserInfoRegisterRequest userInfoRegisterRequest) {
        return null;
    }

    @Override
    public Long saveRole(Long userId) {
        //保存用户角色信息
        UserRole userRole = new UserRole();
        userRole.setUserId(userId);
        userRole.setRoleId(Integer.valueOf(UserRoleEnum.USER.getValue()));
        boolean save = userRoleService.save(userRole);
        if(!save){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"普通用户角色保存失败");
        }
        return userId;
    }

    public long userRegister(String userAccount, String userPassword, String checkPassword) {
        // 1. 校验
        if (StringUtils.isAnyBlank(userAccount, userPassword, checkPassword)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数为空");
        }
        if (userAccount.length() < 4) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "用户账号过短");
        }
        if (userPassword.length() < 8 || checkPassword.length() < 8) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "用户密码过短");
        }
        // 密码和校验密码相同
        if (!userPassword.equals(checkPassword)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "两次输入的密码不一致");
        }
        synchronized (userAccount.intern()) {
            // 账户不能重复
            QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_account", userAccount);
            UserInfo userInfo = userInfoMapper.selectOne(queryWrapper);
            //已经存在基础账号
            if (ObjectUtil.isNotEmpty(userInfo)) {
                throw new BusinessException(ErrorCode.PARAMS_ERROR, "账号重复");
            }
            // 2. 加密
            String encryptPassword = DigestUtils.md5DigestAsHex((SALT + userPassword).getBytes());
            // 3. 插入数据
            UserInfo user = new UserInfo();
            user.setUserAccount(userAccount);
            user.setUserPassword(encryptPassword);
            long userId = userInfoMapper.insert(user);
            return userId;
        }
    }
}
